<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>TypeScript 2 | 个人博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="泛型类与泛型约束泛型类 12345class Log&lt;T&gt; &amp;#123;    run(value: T) &amp;#123;        console.log(value)    &amp;#125;&amp;#125; 注：泛型类不能应用于类的静态成员 123let log1 &#x3D; new Log&lt;number&gt;() &#x2F;&#x2F;为数字泛型    log1.run(1)let log2 &#x3D; new">
<meta property="og:type" content="article">
<meta property="og:title" content="TypeScript 2">
<meta property="og:url" content="https://gitee.com/wieldy/bailment-blog/2023/04/21/TypeScript2/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="泛型类与泛型约束泛型类 12345class Log&lt;T&gt; &amp;#123;    run(value: T) &amp;#123;        console.log(value)    &amp;#125;&amp;#125; 注：泛型类不能应用于类的静态成员 123let log1 &#x3D; new Log&lt;number&gt;() &#x2F;&#x2F;为数字泛型    log1.run(1)let log2 &#x3D; new">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-04-21T06:15:15.000Z">
<meta property="article:modified_time" content="2023-07-03T06:13:28.351Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/bailment-blog/atom.xml" title="个人博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/bailment-blog/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/bailment-blog/css/style.css">

  
    
<link rel="stylesheet" href="/bailment-blog/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/bailment-blog/" id="logo">个人博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/bailment-blog/">Home</a>
        
          <a class="main-nav-link" href="/bailment-blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/bailment-blog/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/wieldy/bailment-blog"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-TypeScript2" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/bailment-blog/2023/04/21/TypeScript2/" class="article-date">
  <time class="dt-published" datetime="2023-04-21T06:15:15.000Z" itemprop="datePublished">2023-04-21</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      TypeScript 2
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="泛型类与泛型约束"><a href="#泛型类与泛型约束" class="headerlink" title="泛型类与泛型约束"></a>泛型类与泛型约束</h2><p>泛型类</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Log</span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="title function_">run</span>(<span class="params">value: T</span>) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(value)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注：泛型类不能应用于类的静态成员</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> log1 = <span class="keyword">new</span> <span class="title class_">Log</span>&lt;<span class="built_in">number</span>&gt;() <span class="comment">//为数字泛型</span></span><br><span class="line">    log1.<span class="title function_">run</span>(<span class="number">1</span>)</span><br><span class="line"><span class="keyword">let</span> log2 = <span class="keyword">new</span> <span class="title class_">Log</span>() <span class="comment">//可传任意类型</span></span><br></pre></td></tr></table></figure>

<p>泛型约束</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> <span class="title class_">Length</span> &#123;</span><br><span class="line">    <span class="attr">length</span>: <span class="built_in">number</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> log&lt;T <span class="keyword">extends</span> <span class="title class_">Length</span>&gt;(<span class="attr">value</span>: T): T &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(value, value.<span class="property">length</span>)</span><br><span class="line">    <span class="keyword">return</span> value</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">log</span>([<span class="number">1</span>]) <span class="title function_">log</span>(<span class="string">&#x27;123&#x27;</span>) <span class="title function_">log</span>(&#123; <span class="attr">length</span>: <span class="number">1</span> &#125;)</span><br></pre></td></tr></table></figure>
<p>泛型的好处：<br>1.函数和类可以轻松地支持多种类型，增加程序的扩展性<br>2.不必写多条函数重载，冗长的联合类型声明，增强代码可读性<br>3.灵活控制类型之间的约束</p>
<h2 id="类型检查机制"><a href="#类型检查机制" class="headerlink" title="类型检查机制"></a>类型检查机制</h2><p>TypeScript编译器在做类型检查时，所秉承的一些原则，以及表现出的一些行为。<br>作用：辅助开发，提升开发效率。<br>1.类型推断<br>2.类型兼容性<br>3.类型保护<br>这一块有不少内容~~~~~ 单独再讲 接下来来讲Typescript中的高级类型</p>
<h2 id="交叉类型与联合类型"><a href="#交叉类型与联合类型" class="headerlink" title="交叉类型与联合类型"></a>交叉类型与联合类型</h2><p>高级类型是指TS未来保障语言的灵活性，引用的一些语言特性。用于多变的开发场景。<br>交叉类型    将多个类型合并为一个类型，新的类型将具有所有类型的特性。</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> <span class="title class_">DogInterface</span> &#123;</span><br><span class="line">    <span class="title function_">run</span>(): <span class="built_in">void</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">CatInterface</span> &#123;</span><br><span class="line">    <span class="title function_">jump</span>(): <span class="built_in">void</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// &amp; 取所有类型的并集</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">pet</span>:<span class="title class_">DogInterface</span> &amp; <span class="title class_">CatInterface</span> = &#123;</span><br><span class="line">    <span class="title function_">run</span>(<span class="params"></span>) &#123;&#125;</span><br><span class="line">    <span class="title function_">jump</span>(<span class="params"></span>) &#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>联合类型    声明的类型并不确定，可以为多个类型中的一个。</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="attr">a</span>:<span class="built_in">number</span> | <span class="built_in">string</span> = <span class="string">&#x27;a&#x27;</span> <span class="comment">// 调用两者的公用方法</span></span><br><span class="line"><span class="comment">// 字面量类型 有时不仅限定一个变量的类型，而且限定变量的取值在某一个特定的范围内。</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">b</span>:<span class="string">&#x27;a&#x27;</span> | <span class="string">&#x27;b&#x27;</span> | <span class="string">&#x27;c&#x27;</span>; <span class="keyword">let</span> <span class="attr">c</span>:<span class="number">1</span> | <span class="number">2</span> | <span class="number">3</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dog</span> <span class="keyword">implements</span> <span class="title class_">DogInterface</span> &#123;</span><br><span class="line">    <span class="title function_">run</span>(<span class="params"></span>) &#123;&#125;</span><br><span class="line">    <span class="title function_">eat</span>(<span class="params"></span>) &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Cat</span> <span class="keyword">implements</span> <span class="title class_">CatInterface</span> &#123;</span><br><span class="line">    <span class="title function_">jump</span>(<span class="params"></span>) &#123;&#125;</span><br><span class="line">    <span class="title function_">eat</span>(<span class="params"></span>) &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">Master</span> &#123; <span class="title class_">Boy</span>, <span class="title class_">Girl</span> &#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">getPet</span>(<span class="params">master: Master</span>) &#123;</span><br><span class="line">    <span class="comment">// pet 为联合类型</span></span><br><span class="line">    <span class="keyword">let</span> pet = master === <span class="title class_">Master</span>.<span class="property">Boy</span> ? <span class="keyword">new</span> <span class="title class_">Dog</span>() : <span class="keyword">new</span> <span class="title class_">Cat</span>()</span><br><span class="line">    pet.<span class="title function_">eat</span>() <span class="comment">//所有类型的交集</span></span><br><span class="line">    <span class="keyword">return</span> pet</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>模式：可区分的联合类型 结合联合类型和字面量类型的一种类型保护方法。<br>核心思想：一个类型如果是多个类型的联合类型，并且每个类型之间有公共的属性，可以根据这个属性创建不同的保护区块。</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> <span class="title class_">Rectangle</span> &#123;</span><br><span class="line">    <span class="attr">kind</span>: <span class="string">&#x27;rectangle&#x27;</span>;</span><br><span class="line">    <span class="attr">width</span>: <span class="built_in">number</span>;</span><br><span class="line">    <span class="attr">height</span>: <span class="built_in">number</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">Square</span> &#123;</span><br><span class="line">    <span class="attr">kind</span>: <span class="string">&#x27;square&#x27;</span>,</span><br><span class="line">    <span class="attr">size</span>: <span class="built_in">number</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">Circle</span> &#123;</span><br><span class="line">    <span class="attr">kind</span>: <span class="string">&#x27;circle&#x27;</span>,</span><br><span class="line">    <span class="attr">r</span>: <span class="built_in">number</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">type</span> <span class="title class_">Shape</span> = <span class="title class_">Rectangle</span> | <span class="title class_">Square</span> | <span class="title class_">Circle</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">area</span>(<span class="params">s: Shape</span>):<span class="built_in">number</span> &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&quot;square&quot;</span>:</span><br><span class="line">        <span class="keyword">return</span> s.<span class="property">size</span> * s.<span class="property">zize</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&quot;rectangle&quot;</span>:</span><br><span class="line">        <span class="keyword">return</span> s.<span class="property">height</span> * s.<span class="property">width</span>;</span><br><span class="line">    <span class="attr">default</span>:</span><br><span class="line">        <span class="keyword">return</span> (<span class="function">(<span class="params">e:<span class="built_in">never</span></span>) =&gt;</span> &#123; <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">Error</span>(e) &#125;)(s)</span><br><span class="line">        <span class="comment">// 如果s为never类型，则前面所有分支都被覆盖</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">area</span>(&#123; <span class="attr">kind</span>: <span class="string">&#x27;circle&#x27;</span>, <span class="attr">r</span>:<span class="number">1</span> &#125;))</span><br></pre></td></tr></table></figure>
<h2 id="高级类型-2-索引类型"><a href="#高级类型-2-索引类型" class="headerlink" title="高级类型(2):索引类型"></a>高级类型(2):索引类型</h2><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">    <span class="attr">a</span>: <span class="number">1</span>,</span><br><span class="line">    <span class="attr">b</span>: <span class="number">2</span>,</span><br><span class="line">    <span class="attr">c</span>: <span class="number">3</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">getValues</span>(<span class="params">obj:<span class="built_in">any</span>, keys: <span class="built_in">string</span>[]</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> keys.<span class="title function_">map</span>(<span class="function"><span class="params">key</span> =&gt;</span> obj[key])</span><br><span class="line">&#125;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">getValues</span>(obj, [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>])) <span class="comment">// [1,2]</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">getValues</span>(ojb, [<span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;f&#x27;</span>])) <span class="comment">// [undefined,undefined] TS并没有进行检查</span></span><br></pre></td></tr></table></figure>
<p>检测obj对象没有e,f属性？引入索引类型<br>1.索引类型的查询操作符<br>含义：表示类型T的所有公共属性的字面量的联合类型。</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">keyof T</span><br></pre></td></tr></table></figure>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> <span class="title class_">Obj</span> &#123;</span><br><span class="line">    <span class="attr">a</span>: <span class="built_in">number</span>,</span><br><span class="line">    <span class="attr">b</span>: <span class="built_in">string</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> <span class="attr">key</span>:keyof obj   <span class="comment">//key为a和b字面量的联合类型</span></span><br><span class="line"><span class="comment">// === let key: number | string</span></span><br></pre></td></tr></table></figure>

<p>2.索引访问操作符 语法:T[k] 对象T的属性k所有代表的类型</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="attr">value</span>:obj[<span class="string">&#x27;a&#x27;</span>]</span><br><span class="line">T <span class="keyword">extends</span> U <span class="comment">// 泛型变量可以继承某个类型获得某些属性</span></span><br></pre></td></tr></table></figure>
<p>改造 getValues函数</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> getValue&lt;T, K <span class="keyword">extends</span> keyof T&gt;(<span class="attr">obj</span>:T,<span class="attr">keys</span>:k[]):T[k][] &#123;</span><br><span class="line">    <span class="keyword">return</span> keys.<span class="title function_">map</span>(<span class="function"><span class="params">key</span> =&gt;</span> obj[key])</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="高级类型（3）：映射类型"><a href="#高级类型（3）：映射类型" class="headerlink" title="高级类型（3）：映射类型"></a>高级类型（3）：映射类型</h2><p>可以从旧的类型生成一个新的类型，比如：把一个属性中所有属性变为只读。<br>例：</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> <span class="title class_">Obj</span> &#123;</span><br><span class="line">    <span class="attr">a</span>: <span class="built_in">string</span>;</span><br><span class="line">    <span class="attr">b</span>: <span class="built_in">number</span>;</span><br><span class="line">    <span class="attr">c</span>: <span class="built_in">boolean</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">type</span> <span class="title class_">ReadonlyObj</span> = <span class="title class_">Readonly</span>&lt;<span class="title class_">Obj</span>&gt;</span><br><span class="line"><span class="comment">// ts内置Readonly实现方法</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">Readonly</span>&lt;T&gt; = &#123;</span><br><span class="line">    <span class="keyword">readonly</span> [P <span class="keyword">in</span> keyof T]: T[P];</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">type</span> <span class="title class_">PartialObj</span> = <span class="title class_">Partial</span>&lt;<span class="title class_">Obj</span>&gt;</span><br><span class="line"><span class="comment">// ts内置Partial实现方法</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">Partial</span>&lt;T&gt; = &#123;</span><br><span class="line">    [p <span class="keyword">in</span> keyof T] ?: T[p];</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// Pick类型 抽取Object的字集</span></span><br><span class="line"><span class="comment">// a和b会单独抽离出来，形成一个新的类型</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">PickObj</span> = <span class="title class_">Pick</span>&lt;<span class="title class_">Obj</span>, <span class="string">&#x27;a&#x27;</span> | <span class="string">&#x27;b&#x27;</span>&gt;</span><br><span class="line"><span class="keyword">type</span> <span class="title class_">Pick</span>&lt;T,K <span class="keyword">extends</span> keyof T&gt; = &#123;</span><br><span class="line">    [p <span class="keyword">in</span> k]:T[p];</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">type</span> <span class="title class_">RecordObj</span> = <span class="title class_">Record</span>&lt;<span class="string">&#x27;x&#x27;</span> | <span class="string">&#x27;y&#x27;</span>, <span class="title class_">Obj</span>&gt;</span><br></pre></td></tr></table></figure>
<h2 id="高级类型（4）：条件类型"><a href="#高级类型（4）：条件类型" class="headerlink" title="高级类型（4）：条件类型"></a>高级类型（4）：条件类型</h2><p>是一种由条件表达式所确定的类型。<br>表达形式 &#x3D;&gt; T extends U ? X : Y<br>如果类型T可以被赋值给类型U，哪结果类型为X类型，否者为Y类型。</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">TypeName</span>&lt;T&gt; = </span><br><span class="line">    T <span class="keyword">extends</span> <span class="built_in">string</span> ? <span class="string">&quot;string&quot;</span> :</span><br><span class="line">    T <span class="keyword">extends</span> <span class="built_in">number</span> ? <span class="string">&quot;number&quot;</span> :</span><br><span class="line">    T <span class="keyword">extends</span> <span class="built_in">boolean</span> ? <span class="string">&quot;boolean&quot;</span> :</span><br><span class="line">    T <span class="keyword">extends</span> <span class="literal">undefined</span> ? <span class="string">&quot;undefined&quot;</span> :</span><br><span class="line">    T <span class="keyword">extends</span> <span class="title class_">Function</span> ? <span class="string">&quot;Function&quot;</span> :</span><br><span class="line">    <span class="string">&quot;object&quot;</span>;</span><br><span class="line"><span class="keyword">type</span> <span class="variable constant_">T1</span> = <span class="title class_">TypeName</span>&lt;<span class="built_in">string</span>&gt; <span class="comment">//推断为：string</span></span><br><span class="line"><span class="keyword">type</span> <span class="variable constant_">T2</span> = <span class="title class_">TypeName</span>&lt;<span class="built_in">string</span>[]&gt; <span class="comment">//字面量类型值为T2=&quot;object&quot;</span></span><br><span class="line"><span class="comment">// 分布式条件类型</span></span><br><span class="line"><span class="comment">// (A | B) extends U ? X : Y  //拆解为</span></span><br><span class="line"><span class="comment">// (A extends U ? X : Y) | (B extends U ? X : Y)</span></span><br><span class="line"><span class="keyword">type</span> <span class="variable constant_">T3</span> = <span class="title class_">TypeName</span>&lt;<span class="built_in">string</span> | <span class="built_in">string</span>[]&gt;</span><br><span class="line"><span class="keyword">type</span> <span class="title class_">Diff</span>&lt;T,U&gt; = T <span class="keyword">extends</span> U ? <span class="built_in">never</span> : T</span><br><span class="line"><span class="keyword">type</span> <span class="variable constant_">T4</span> = <span class="title class_">Diff</span>&lt;<span class="string">&#x27;a&#x27;</span>| <span class="string">&#x27;b&#x27;</span> | <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;a&#x27;</span> | <span class="string">&#x27;e&#x27;</span>&gt;</span><br><span class="line"><span class="comment">// Diff&lt;&#x27;a&#x27;, &#x27;a&#x27; | &#x27;e&#x27;&gt; | Diff&lt;&#x27;b&#x27;, &#x27;a&#x27;|&#x27;e&#x27;&gt; | Diff&lt;&#x27;c&#x27;, &#x27;a&#x27;|&#x27;e&#x27;&gt;</span></span><br><span class="line"><span class="comment">// never | &#x27;b&#x27; | &#x27;c&#x27;</span></span><br><span class="line"><span class="comment">// &#x27;b&#x27; | &#x27;c&#x27;</span></span><br></pre></td></tr></table></figure>
<p>从类型T中可以过滤掉类型U的类型</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">NotNull</span>&lt;T&gt; = <span class="title class_">Diff</span>&lt;T, <span class="literal">undefined</span> | <span class="literal">null</span>&gt;</span><br><span class="line"><span class="keyword">type</span> <span class="variable constant_">T5</span> = <span class="title class_">NotNull</span>&lt;<span class="built_in">string</span> | <span class="built_in">number</span> | <span class="literal">undefined</span> | <span class="literal">null</span>&gt;</span><br><span class="line"><span class="title class_">Exclude</span>&lt;T, U&gt; <span class="comment">//从类型T中过滤掉可以赋值给类型U的类型</span></span><br><span class="line"><span class="title class_">NonNullable</span>&lt;T&gt;</span><br><span class="line"><span class="title class_">Extract</span>&lt;T, U&gt; <span class="comment">//可以从类型T中抽取出可以赋值给U的类型</span></span><br><span class="line"><span class="keyword">type</span> <span class="variable constant_">T6</span> = <span class="title class_">Extract</span>&lt;<span class="string">&#x27;a&#x27;</span> | <span class="string">&#x27;b&#x27;</span> | <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;a&#x27;</span> | <span class="string">&#x27;e&#x27;</span>&gt;</span><br><span class="line"><span class="title class_">RetrunType</span>&lt;T&gt; <span class="comment">// 可以获取一个函数返回值的类型</span></span><br><span class="line"><span class="keyword">type</span> <span class="variable constant_">T7</span> = <span class="title class_">ReturnType</span>&lt;<span class="function">()=&gt;</span><span class="built_in">string</span>&gt;</span><br><span class="line"><span class="comment">// infer 待推断或延迟推断</span></span><br></pre></td></tr></table></figure>

<h2 id="列举一些常用的辅助函数"><a href="#列举一些常用的辅助函数" class="headerlink" title="列举一些常用的辅助函数"></a>列举一些常用的辅助函数</h2><p>TypeScript 提供了很多辅助函数来帮助我们处理类型。这里列举一些常用的 TypeScript 辅助函数：<br>Partial<T>：将类型 T 的所有属性设置为可选属性。<br>Required<T>：将类型 T 的所有属性设置为必选属性。<br>Readonly<T>：将类型 T 的所有属性设置为只读属性。<br>Record&lt;K, T&gt;：返回一个包含类型为 T 的值的对象，该对象的键为类型为 K 的值。<br>Pick&lt;T, K&gt;：从类型 T 中选取一组属性 K 并返回一个新的类型。<br>Exclude&lt;T, U&gt;：从类型 T 中排除类型 U 的所有属性。<br>Extract&lt;T, U&gt;：从类型 T 中提取类型 U 的所有属性。<br>Omit&lt;T, K&gt;：从类型 T 中移除属性 K 并返回一个新的类型。<br>NonNullable<T>：从类型 T 中移除 null 和 undefined 并返回一个新的类型。<br>ReturnType<T>：返回函数类型 T 的返回类型。<br>Parameters<T>：返回函数类型 T 的参数类型。<br>ConstructorParameters<T>：返回构造函数类型 T 的参数类型。<br>InstanceType<T>：返回构造函数类型 T 的实例类型。<br>RequiredKeys<T>：返回类型 T 中所有必须的属性的键的联合类型。<br>OptionalKeys<T>：返回类型 T 中所有可选的属性的键的联合类型。<br>这些辅助函数可以使我们更加方便地定义和操作 TypeScript 类型。当然，还有很多其他的辅助函数，具体使用时需要根据实际情况选择合适的函数。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/wieldy/bailment-blog/2023/04/21/TypeScript2/" data-id="cljmgj1eu000azc9jerna7vae" data-title="TypeScript 2" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/bailment-blog/2023/04/23/vue3-2.reactivity/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          2.reactivity
        
      </div>
    </a>
  
  
    <a href="/bailment-blog/2023/04/20/TypeScript/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">TypeScript</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/06/">June 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/04/">April 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/03/">March 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2022/03/">March 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/bailment-blog/2023/07/03/%E5%B7%A5%E4%BD%9C%E6%97%A5%E5%BF%97/">工作日志</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/29/payMent/">payMent</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/20/%E7%BD%91%E7%BA%A2%E9%9D%A2%E8%AF%95%E9%A2%98/">原理题</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/19/%E5%89%8D%E7%AB%AF%E5%A4%A7%E5%B1%8F%E8%87%AA%E9%80%82%E5%BA%94%E6%96%B9%E6%B3%95/">前端大屏自适应方法</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/17/10%E4%B8%AAJavaScript%E5%B8%B8%E8%A7%81%E9%AB%98%E7%BA%A7%E7%9F%A5%E8%AF%86%E7%82%B9/">10个JavaScript常见高级知识点</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/bailment-blog/" class="mobile-nav-link">Home</a>
  
    <a href="/bailment-blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/bailment-blog/js/jquery-3.4.1.min.js"></script>



  
<script src="/bailment-blog/fancybox/jquery.fancybox.min.js"></script>




<script src="/bailment-blog/js/script.js"></script>





  </div>
</body>
</html>